/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import entity.User;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.DisabledUserManager;
import manager.UserManager;

/**
 *
 * @author User
 */
public class UserDB implements IManagerDB<UserManager, DisabledUserManager> {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public UserDB(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void loadData(UserManager t, boolean value) {
        try {
            cstmt = conn.prepareCall("{call sp_GetUser}");
            cstmt.setBoolean("Status", value);
            rs = cstmt.executeQuery();
            while (rs.next()) {
                String userName = rs.getString("UserName");
                String passWord = rs.getString("PassWord");
                boolean authorization = rs.getBoolean("Authorization");
                boolean status = rs.getBoolean("Status");

                User user = new User(userName, passWord, authorization, status);
                t.add(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void insert(Object object) {
        User user = (User) object;
        try {
            cstmt = conn.prepareCall("{call sp_InsertUser(?,?,?,?)}");
            cstmt.setString(1, user.getUserName());
            cstmt.setString(2, user.getPassWord());
            cstmt.setBoolean(3, user.isAuthorization());
            cstmt.setBoolean(4, user.isStatus());


            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(UserDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
        User user = (User) object;
        try {
            cstmt = conn.prepareCall("{call sp_UpdateUser(?,?,?,?)}");
            cstmt.setString(1, user.getUserName());
            cstmt.setString(2, user.getPassWord());
            cstmt.setBoolean(3, user.isAuthorization());
            cstmt.setBoolean(4, user.isStatus());


            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(UserDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
